package jp.tkym.labs.gwt.client;

import java.util.Date;

import jp.tkym.labs.gwt.client.gantt.TaskSchedule;
import jp.tkym.labs.gwt.client.gantt.GanttTableModelBuilder;
import jp.tkym.labs.gwt.client.gantt.GwtCalendar;
import jp.tkym.labs.gwt.client.service.GanttService;
import jp.tkym.labs.gwt.client.service.GanttServiceAsync;
import jp.tkym.labs.gwt.client.table.FlexTableBindModel;
import jp.tkym.labs.gwt.client.table.FlexTableBinder;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Labs implements EntryPoint {

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final GanttServiceAsync ganttService = GWT.create(GanttService.class);
	
	public static DialogBox alertWidget(final String header, final String content) {
        final DialogBox box = new DialogBox();
        final VerticalPanel panel = new VerticalPanel();
        box.setText(header);
        panel.add(new Label(content));
        final Button buttonClose = new Button("Close",new ClickHandler() {
			public void onClick(ClickEvent arg0) {
                box.hide();
			}
		});
        // few empty labels to make widget larger
        final Label emptyLabel = new Label("");
        emptyLabel.setSize("auto","25px");
        panel.add(emptyLabel);
        panel.add(emptyLabel);
        buttonClose.setWidth("90px");
        panel.add(buttonClose);
        panel.setCellHorizontalAlignment(buttonClose, HasAlignment.ALIGN_RIGHT);
        box.add(panel);
        return box;
    }
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final FlexTable table3 = new FlexTable();
		table3.setStyleName("ganttTable");
		
		final GanttTableModelBuilder builder = new GanttTableModelBuilder();
		final FlexTableBindModel headerModel = new FlexTableBindModel(); 
		final FlexTableBinder headerBinder = new FlexTableBinder(table3, headerModel, 0, 1);
		final FlexTableBindModel ganttModel = new FlexTableBindModel(); 
		final FlexTableBinder ganttBinder = new FlexTableBinder(table3, ganttModel, 2, 1);
		
		Button nextButton = new Button(">", new ClickHandler() {
			public void onClick(ClickEvent event) {
				builder.addDays(1);
				builder.header(headerModel);
				headerBinder.bind();
				builder.gantt(ganttModel);
				ganttBinder.bind();
			}
		});
		builder.setNextButton(nextButton);
		
		Button backButton = new Button("<", new ClickHandler() {
			public void onClick(ClickEvent event) {
				builder.addDays(-1);
				builder.header(headerModel);
				headerBinder.bind();
				builder.gantt(ganttModel);
				ganttBinder.bind();
			}
		});
		builder.setBackButton(backButton);

		builder.header(headerModel);
		headerBinder.bind();
		
		builder.addGantt(createGantt("task001", 0, 12));
		builder.addGantt(createGantt("task002", -3, 5));
		builder.addGantt(createGantt("task003", 2, 7));
		builder.addGantt(createGantt("task004", 3, 10));
		builder.addGantt(createGantt("task005", 4, 11));
		builder.addGantt(createGantt("task006", 5, 12));
		builder.addGantt(createGantt("task007", 6, 13));
		
		builder.gantt(ganttModel);
		ganttBinder.bind();
		
		RootPanel.get("test1-region").add(table3);
	}
	
	private TaskSchedule createGantt(String taskName, int startAdd, int endAdd){
		Widget taskWidget = new Label(taskName);
		Date start = GwtCalendar.dateFromAddDays(startAdd);
		Date end = GwtCalendar.dateFromAddDays(endAdd);
		return new GanttDummy(taskWidget, start, end);
	}
	
	private class GanttDummy implements TaskSchedule{
		private Widget taskWidget;
		private Date start;
		private Date end;
		
		GanttDummy(Widget taskWidget, Date start, Date end){
			this.taskWidget = taskWidget;
			this.start = start;
			this.end = end;
		}
		
		public Date getStart() {
			return this.start;
		}
		public Date getEnd() {
			return this.end;
		}
		public Widget getTaskWidget() {
			return taskWidget;
		}
	}
}